package com.irdstudio.efp.esb.service.impl.hj;

import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.DAY;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.HOUR;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.MIN;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.MONTH;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SECOND;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.SEQ;
import static com.irdstudio.basic.sequence.service.DefaultPatternDescriptor.YEAR_2;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.sequence.service.DefaultPatternDescriptor;
import com.irdstudio.basic.sequence.service.PatternDescriptor;
import com.irdstudio.basic.sequence.service.facade.PatternedLimitableSeqService;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.service.bo.req.hj.ReqCusInfoBatchCreateBean;
import com.irdstudio.efp.esb.service.bo.resp.hj.ResqCusInfoBatchCreateBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.hj.CusInfoBatchCreateService;

/**
 * 
 * 批量建立互金客户
 * 
 * 
 * @author ludayong
 * @since 2019年5月20日 下午2:53:36
 * @version 1.0
 */
@Service("cusInfoBatchCreateService")
public class CusInfoBatchCreateServiceImpl implements CusInfoBatchCreateService {

	Logger logger = LoggerFactory.getLogger(CustomerInfoCreateServiceImpl.class);

	/**
	 * 
	 * 批量建立互金客户 <br>
	 * 0.1:ludayong:2019年5月20日 下午2:58:49:新增 <br>
	 * 
	 * @author ludayong
	 * @since 2019年5月20日 下午2:58:49
	 * @date 2019年5月20日 下午2:58:49
	 * @version 0.1
	 */
	@Override
	public boolean downLoadFileNotice(ReqCusInfoBatchCreateBean reqCusInfoBatchCreateBean) {
		try {
			//生成ESB系统的全局流水号(GlblSrlNo)
			String glblSrlNo = generateESBTradeSerialNo();
			//设置ESB系统的全局流水号(GlblSrlNo)
			TraceUtil.setTraceId(glblSrlNo);
		} catch (Exception e1) {
			e1.printStackTrace();
			logger.info("生成ESB系统的全局流水号(GlblSrlNo)出错，错误信息：" +e1.getMessage());
			return false;
		}
		// 设置贷款文件下载通知-平台ID
		TraceUtil.setPlafmID(ESBClientConstance.bdPlafmID);
		// 设置贷款文件下载通知-机构Id
		TraceUtil.setInstId(ESBClientConstance.bdInstId);
		// 批量建立互金客户响应信息
		ResqCusInfoBatchCreateBean rspBean = new ResqCusInfoBatchCreateBean();

		boolean result = false;
		logger.info("通知互金批量建立互金客户开始，文件名：" + reqCusInfoBatchCreateBean.getFileNm());
		try {
			EsbRespServiceBean recvBean = ESBClientFactory.buildClient().withBody(reqCusInfoBatchCreateBean).withTradeNo("50120004")
					.withScene("01").create().sendAndReceive();
			// String retCd = esbRespSysHeadBean.getRetInfArry().get(0).getRetCd();
			rspBean = recvBean.getBody(ResqCusInfoBatchCreateBean.class);
			if (Objects.nonNull(rspBean)) {
				logger.info("通知互金批量建立互金客户成功，文件名：" + reqCusInfoBatchCreateBean.getFileNm() + "，响应信息：" + JSONObject.toJSONString(rspBean));
				result = true;
			} else {
				logger.info("通知互金批量建立互金客户失败，文件名：" + reqCusInfoBatchCreateBean.getFileNm());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.info("通知互金批量建立互金客户失败，文件名：" + reqCusInfoBatchCreateBean.getFileNm());
			result = false;
		} finally {
			// 清除全局链路跟踪信息
			TraceUtil.clear();
		}
		logger.info("通知互金批量建立互金客户结束，文件名：" + reqCusInfoBatchCreateBean.getFileNm());
		return result;
	}
	
	/* 生成ESB系统的全局流水号(GlblSrlNo)规则  */
	private static final PatternDescriptor pattern = 
			DefaultPatternDescriptor.of(ESBClientConstance.CnsmrSysNo + 
					YEAR_2 + MONTH + DAY + HOUR + MIN + SECOND + SEQ, 6);
	
	/* 生成ESB系统的全局流水号(GlblSrlNo) */
	private String generateESBTradeSerialNo() throws Exception {
		PatternedLimitableSeqService sequenceService = SpringContextUtils.getBean(PatternedLimitableSeqService.class);
		return sequenceService.getPatternedSequence("ESB", pattern);
	}
}
